home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / users / userinfo / userinfo.cpp < prev    next >
C/C++ Source or Header  |  1996-07-10  |  8KB  |  365 lines

  1. // USERINFO (1.3) will get all the good bindery info about a user.
  2. // It will tell the users full name, groups,
  3. // and security equivalences. (If possible)
  4. // Wild cards (*,?) accepted.
  5. //
  6. // If the user is logged in, it tells connection num,
  7. // network number, node number, login date & time
  8. //
  9. // By:        Douglas R. Cannon
  10. // On:        6/11/1992
  11. // email:      dougc@bert.cs.byu.edu
  12. //
  13.  
  14.  
  15. #include <iostream.h>
  16. #include <dos.h>
  17. #include <conio.h>
  18. #include "obs.h"
  19.  
  20.  
  21. #define WILD 0xFF
  22. #define MAX_GROUPS 30
  23.  
  24.  
  25. struct {
  26.   nw_long id;
  27.   char name[48];
  28.   char full_name[48];
  29.   int num_groups;
  30.   char groups[MAX_GROUPS][48];
  31.  
  32.   int num_security;
  33.   struct {
  34.     char object[48];
  35.     nw_int type;
  36.   } security[MAX_GROUPS];
  37.  
  38. } user;
  39.  
  40.  
  41. nw_int user_type,user_group;
  42. nw_long last_id;
  43.  
  44. void help();
  45. int find_user(char *);
  46. void get_info();
  47. void print_info();
  48. void print_id(unsigned char);
  49. void print_connections(connections *);
  50.  
  51.  
  52.  
  53. void main(int argc, char *argv[])
  54. {
  55.   char c,temp[48],cmd[80];
  56.   short continuous = 0,done=0,n=1,cm=2;
  57.  
  58.   if ((argc < 2) || (argc > 3)) help();
  59.  
  60.   if (argc == 3) {
  61.  
  62.     if (argv[1][0] == '/') {
  63.       cm = 1;
  64.       n = 2;
  65.     }
  66.  
  67.     strcpy(cmd,argv[cm]);
  68.     strupr(cmd);
  69.     if (strcmp(cmd,"/C"))  help();
  70.     else continuous = 1;
  71.  
  72.   }
  73.  
  74.  
  75.   if ((argc == 2) || ((continuous) && (argc == 3))) {
  76.  
  77.     cout << "\n----------------------------------------\n";
  78.  
  79.     user_type.high_byte = 0x00;
  80.     user_type.low_byte = 0x01;
  81.     user_group.high_byte = 0x00;
  82.     user_group.low_byte = 0x02;
  83.  
  84.     strcpy(temp,argv[n]);
  85.     strupr(temp);
  86.  
  87.     last_id.highest_byte = WILD;
  88.     last_id.higher_byte = WILD;
  89.     last_id.lower_byte = WILD;
  90.     last_id.lowest_byte = WILD;
  91.  
  92.     if (find_user(temp))
  93.       cout << "\nUser: " << temp << " not found.\n";
  94.  
  95.     else do {
  96.  
  97.       get_info();
  98.       cout << "----------------------------------------\n";
  99.       done = find_user(temp);
  100.       if (!done) {
  101.     if (!continuous) {
  102.       cout << "More... press a key, or press 'C' for continuous.";
  103.       c = toupper(getch());
  104.       cout << '\r';
  105.       clreol();
  106.       if (c=='C') continuous = 1;
  107.       if (c=='Q') done = 1;
  108.     }
  109.       }
  110.  
  111.     } while (!done);
  112.  
  113.  
  114.   }
  115.  
  116. }
  117.  
  118.  
  119.  
  120. void help()
  121. {
  122.   cout << "\n\n"
  123.        << "UserInfo (ver 1.3)\n"
  124.        << "By: Douglas R. Cannon (dougc@bert.cs.byu.edu)\n\n"
  125.        << "Usage: UserInfo <user_name> </C (optional) for Continuous.>\n\n";
  126.  
  127. }
  128.  
  129.  
  130.  
  131.  
  132. // find_user will look for a user.
  133. // The nw_long last_id it uses is a global var.
  134. // It must previously be set to WILD, or set to
  135. // the last user found.  This enables wild card searches.
  136. // if it finds one, it puts the name in user.name
  137. // and the object_id in user.id, and sets last_id to
  138. // the object_id.  Then it returns a 0
  139. //
  140. // if it can't find the user, it returns a 1
  141.  
  142. int find_user(char *name)
  143. {
  144.   int al;
  145.  
  146.   has_props buf;
  147.  
  148.   al = scan_for_object(last_id,name,user_type,&buf);
  149.  
  150.   if ((!al) && (buf.object_type.low_byte == user_type.low_byte)) {
  151.     user.id = buf.object_id;
  152.     strcpy(user.name,buf.object_name);
  153.  
  154.     last_id = buf.object_id;
  155.  
  156.     return 0;
  157.   }
  158.  
  159.   else return 1;
  160.  
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. // get_info gets the user's full name, groups, and security
  168. // equivalences of the user (if the user running UserInfo is
  169. // privilaged).
  170. // get_info will then call print_info to print the info, then
  171. // check to see if the user is logged in.  If yes, call
  172. // print_connections to print login info.
  173.  
  174. void get_info()
  175. {
  176.   int x,err;
  177.   nw_long zero,id;
  178.   byte val[128];
  179.  
  180.   zero.highest_byte = 0x00;
  181.   zero.higher_byte = 0x00;
  182.   zero.lower_byte = 0x00;
  183.   zero.lowest_byte = 0x00;
  184.  
  185.   err = read_property_value(user.name,user_type,1,"IDENTIFICATION",val);
  186.   if (!err) strcpy(user.full_name,val);
  187.   else user.full_name[0]=NULL;
  188.  
  189.   read_property_value(user.name,user_type,1,"GROUPS_I'M_IN",val);
  190.   x=0;
  191.   user.num_groups=0;
  192.   do {
  193.  
  194.     id.highest_byte = val[x++];
  195.     id.higher_byte = val[x++];
  196.     id.lower_byte = val[x++];
  197.     id.lowest_byte = val[x++];
  198.  
  199.     if (!nw_long_equal(id,zero)) {
  200.       object_name temp;
  201.       get_object_name(id,&temp);
  202.       strcpy(user.groups[user.num_groups++],temp.object_name);
  203.     }
  204.  
  205.   } while (!nw_long_equal(id,zero));
  206.  
  207.   user.num_security=0;
  208.   if (!read_property_value(user.name,user_type,1,"SECURITY_EQUALS",val)) {
  209.     x=0;
  210.     do {
  211.  
  212.       id.highest_byte = val[x++];
  213.       id.higher_byte = val[x++];
  214.       id.lower_byte = val[x++];
  215.       id.lowest_byte = val[x++];
  216.  
  217.       if (!nw_long_equal(id,zero)) {
  218.     object_name temp;
  219.     get_object_name(id,&temp);
  220.     strcpy(user.security[user.num_security].object,temp.object_name);
  221.     user.security[user.num_security++].type = temp.object_type;
  222.       }
  223.  
  224.     } while (!nw_long_equal(id,zero));
  225.  
  226.   }
  227.  
  228.   print_info();
  229.  
  230.   connections cons;
  231.   get_connection_numbers(user_type,user.name,&cons);
  232.   if (cons.number_of_connections) print_connections(&cons);
  233.  
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241. // print_info prints all the info we just found about the
  242. // user excluding any connection or login info.
  243.  
  244. void print_info()
  245. {
  246.   int x;
  247.  
  248.   cout << "Username:  " << user.name;
  249.  
  250.   printf("   (");
  251.   print_id(user.id.highest_byte);
  252.   print_id(user.id.higher_byte);
  253.   print_id(user.id.lower_byte);
  254.   print_id(user.id.lowest_byte);
  255.   printf(")");
  256.  
  257.   cout <<   "\nFull Name: " << user.full_name;
  258.  
  259.  
  260.   for (x=0; x<user.num_groups; x++) {
  261.  
  262.     if (!x) cout << "\nGroups:    ";
  263.     else    cout << "\n           ";
  264.  
  265.     cout << user.groups[x];
  266.   }
  267.  
  268.   if (user.num_security) {
  269.  
  270.     for (x=0; x<user.num_security; x++) {
  271.  
  272.       if (!x) cout << "\nSecurity:  ";
  273.       else    cout << "\n           ";
  274.  
  275.       cout << user.security[x].object << " ";
  276.  
  277.       if (user.security[x].type.low_byte == 0x01)
  278.     cout << "User";
  279.       else if (user.security[x].type.low_byte == 0x02)
  280.     cout << "Group";
  281.        else cout << "Type not defined here";
  282.     }
  283.   }
  284.  
  285.   cout << "\n";
  286. }
  287.  
  288.  
  289. void print_id(unsigned char x)
  290. {
  291.   if (x < 0x10u) printf("0");
  292.   printf("%X",x);
  293. }
  294.  
  295.  
  296. // print_connections prints all connection numbers of the
  297. // user including network,node,and login date and time info.
  298. // print_connections will make sure that get_internet_address
  299. // does not return an error.  If it does, print_connections
  300. // prints 'Unknown' for node and network numbers.
  301.  
  302. void print_connections(connections *cons)
  303. {
  304.   int x,y;
  305.   address addr;
  306.   object_info stuff;
  307.   enum {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND,DOW};
  308.  
  309.   cout << "\n" << user.name << " is currently logged in:\n\n";
  310.  
  311.   for (x=0; x<cons->number_of_connections; x++) {
  312.     cout << "  ";
  313.     if (cons->connection_numbers[x] < 100) cout << ' ';
  314.     if (cons->connection_numbers[x] < 10) cout << ' ';
  315.     printf("%d    ",cons->connection_numbers[x]);
  316.  
  317.     for (y=0; y<14; y++) {
  318.       if (y>=strlen(user.name)) cout << ' ';
  319.       else cout << user.name[y];
  320.     }
  321.     cout << "  ";
  322.  
  323.     if(!get_internet_address(cons->connection_numbers[x],&addr)) {
  324.  
  325.       for (y=0; y<4; y++) {
  326.     if (addr.network_number[y] < 0x10) cout << '0';
  327.     printf("%X",addr.network_number[y]);
  328.       }
  329.  
  330.       cout << "  ";
  331.  
  332.       for (y=0; y<6; y++) {
  333.     if (addr.node_address[y] < 0x10) cout << '0';
  334.     printf("%X",addr.node_address[y]);
  335.       }
  336.  
  337.     }
  338.  
  339.     else cout << " Unknown       Unknown";
  340.  
  341.     cout << "   ";
  342.  
  343.     get_connection_information(cons->connection_numbers[x],&stuff);
  344.  
  345.     y=1900+stuff.login_time[YEAR];
  346.     if (stuff.login_time[MONTH] < 10) cout << ' ';
  347.     if (stuff.login_time[DAY] < 10) cout << ' ';
  348.     printf("%d-%d-%d",stuff.login_time[MONTH],stuff.login_time[DAY],y);
  349.  
  350.     if (stuff.login_time[HOUR] < 10) cout << ' ';
  351.     printf("  %d:",stuff.login_time[HOUR]);
  352.     if (stuff.login_time[MINUTE] <10) cout << '0';
  353.     printf("%d\n",stuff.login_time[MINUTE]);
  354.  
  355.   }
  356.  
  357. }
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.